package com.hw.one.dam2.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hw.one.dam2.entity.*;
import com.hw.one.dam2.service.*;
import com.hw.one.dam2.util.UserSiteUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hw.one.core.controller.BaseController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.dam2.common.DamConstant;
import com.hw.one.dam2.common.DataCalculateTypeEnum;
import com.hw.one.dam2.common.DataTypeEnum;
import com.hw.one.dam2.common.ScopeTypeEnum;
import com.hw.one.dam2.common.SiteTypeEnum;
import com.hw.one.dam2.common.SortScopeTypeEnum;
import com.hw.one.dam2.util.CalendarUtil;
import com.hw.one.dam2.util.ExcelUtil;
import com.hw.one.dam2.util.ResponseUtil;
import com.hw.one.ext.util.UserUtil;
import com.hw.one.util.DateTimeUtil;
import org.springframework.web.bind.annotation.RestController;

/**
 * Copyright 2017 汉威.智慧环保事业部.
 * Created by 李亚坤 on 2017/03/28 10:02.
 * <p>
 * 导出Excel 控制层
 */
@RestController
@RequestMapping(value = "exportExcel")
public class ExportExcelController extends BaseController {


    @Autowired
    private IGPointAirService gPointAirService;

    @Autowired
    private ISmallAirService smallAirService;

    @Autowired
    private ICityAirService cityAirService;

    @Autowired
    private IIndustryPollutionService industryPollutionService;

    @Autowired
    private IWaterPollutionService waterPollutionService;

    @Autowired
    private IWaterSurfaceService waterSurfaceService;

    @Autowired
    private ILampBlackService lampBlackService;

    /**
     * 导出空气质量日报Excel表格
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "airQualityDayReportExportExcel", method = RequestMethod.GET)
    public void airQualityDayReportExportExcel(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {
            // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
            String pointType = request.getParameter("pointType");
            // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
            if (null == pointType || "".equals(pointType)) {
                // return MsgResult.infoResult("点位类型pointType的值为空，查询失败。");
                pointType = SiteTypeEnum.NATIONAL_SITE.getValue();
            }
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            // 开始时间
            String startTime = request.getParameter("startTime");
            startTime = null == startTime || "".equals(startTime)
                ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD) : startTime;
            // 结束时间
            String endTime = request.getParameter("endTime");
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            endTime = null == endTime || "".equals(endTime)
                ? DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD) : endTime;

            // 查询条件
            Map<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put("pointType", pointType);
            paraMap.put("startTime", startTime);
            paraMap.put("endTime", endTime);
            // 数据类型，查询日数据
            paraMap.put("dataType", "dayData");
            // 数据计算类型，实时数据
            paraMap.put("dataCalculateType", "realTimeData");
            paraMap.put("pointCodes", request.getParameter("pointCodes"));
            List<AQIEntity> aqiList = null;

            // 国控点 或者 省控点 或者 小型站; 判断用户是否具有访问权限
            if ((SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("001"))
                || (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("002"))
                || (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("004"))) {
                aqiList = gPointAirService.findAirSiteAQIByTime(paraMap);
                // 无效类型
            } else {
                aqiList = new ArrayList<AQIEntity>();
            }
            String siteStr = "";
            if (SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType)) {
                siteStr = "国控站";
            } else if (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType)) {
                siteStr = "省控站";
            } else if (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType)) {
                siteStr = "小型站";
            }
            String startTimeStr = startTime.split("-")[1] + "月" + startTime.split(" ")[0].split("-")[2] + "日";
            Map<String, Object> region = UserUtil.getRegion();
            String regionName = (String) region.get("regionName");
            wb = ExcelUtil.fillDayReportData2ExcelWithTemplate(aqiList, "dayReportTemplate.xlsx",
                regionName + siteStr + startTimeStr + "空气质量日报");

            ResponseUtil.export(response, wb, regionName + siteStr + startTimeStr + "空气质量日报.xlsx");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出空气质量日报Excel异常,后台异常", e);
        }

    }

    /**
     * 空气质量周报导出Excel
     *
     * @param request
     * @param response
     * @author 李亚坤
     * @createtime 2017年3月28日13:02:12
     */
    @RequestMapping(value = "airQualityWeekReportExcelExport", method = RequestMethod.GET)
    public void airQualityWeekReportExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {
            // 年份
            String year = request.getParameter("year");
            if (null == year || "".equals(year)) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                year = String.valueOf(calendar.get(Calendar.YEAR));
            }
            // 第几周
            String weekNum = request.getParameter("weekNum");
            if (null == weekNum || "".equals(weekNum)) {
                weekNum = String.valueOf(CalendarUtil.getWeekOfYear(new Date()) - 1);
            }

            // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
            String pointType = request.getParameter("pointType");
            // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
            if (null == pointType || "".equals(pointType)) {
                // return MsgResult.infoResult("查询条件点位类型pointType为空，查询失败。");
                pointType = SiteTypeEnum.NATIONAL_SITE.getValue();
            }

            // 封装request中的查询条件
            Map<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put("year", year);
            paraMap.put("weekNum", weekNum);
            paraMap.put("pointType", pointType);
            paraMap.put("pointCodes", request.getParameter("pointCodes"));

            List<IAQIEntity> weekList = null;
            // 国控点 或者 省控点 或者 小型站; 判断用户是否具有访问权限
            if ((SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("001"))
                || (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("002"))
                || (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("004"))) {
                weekList = gPointAirService.findGPointWeekIAQIBySiteCodeAndTime(paraMap);
                // 无效的点位类型
            } else {
                weekList = new ArrayList<IAQIEntity>();
            }
            String siteStr = "";
            if (SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType)) {
                siteStr = "国控站";
            } else if (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType)) {
                siteStr = "省控站";
            } else if (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType)) {
                siteStr = "小型站";
            }
            Map<String, Object> region = UserUtil.getRegion();
            String regionName = (String) region.get("regionName");
            wb = ExcelUtil.fillWeekReportData2ExcelWithTemplate(weekList, "weekReportTemplate.xlsx",
                regionName + siteStr + year + "年第" + weekNum + "周空气质量周报");

            ResponseUtil.export(response, wb, regionName + siteStr + year + "年第" + weekNum + "周空气质量周报" + ".xlsx");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出空气质量周报Excel异常,后台异常", e);
        }
    }

    /**
     * 导出空气质量同环比分析Excel
     *
     * @param request
     * @param response
     * @author 李亚坤
     * @createtime 2017年3月28日14:38:40
     */
    @RequestMapping(value = "airQualityContrastRatioExcelExport", method = RequestMethod.GET)
    public void airQualityContrastRatioExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {
            // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
            String pointType = request.getParameter("pointType");
            // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
            if (null == pointType || "".equals(pointType)) {
                pointType = SiteTypeEnum.NATIONAL_SITE.getValue();
            }
            // 数据类型 周报 weekData;月报monthData;年报yearData
            String dataType = request.getParameter("dataType");
            // 数据类型 默认 周报 DataTypeEnum.WEEK_DATA.getValue()
            if (null == dataType || "".equals(dataType)) {
                dataType = DataTypeEnum.WEEK_DATA.getValue();
            }

            // 年份
            String yearNum = request.getParameter("yearNum");
            if (null == yearNum || "".equals(yearNum)) {
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(new Date());
                yearNum = String.valueOf(calendar.get(Calendar.YEAR));
            }

            // 周数
            String monthOrWeekNum = request.getParameter("monthOrWeekNum");
            if (null == monthOrWeekNum || "".equals(monthOrWeekNum)) {
                monthOrWeekNum = String.valueOf(CalendarUtil.getWeekOfYear(new Date()) - 1);
            }

            // 封装request中的查询条件
            Map<String, Object> paraMap = new HashMap<String, Object>();
            paraMap.put("pointType", pointType);
            paraMap.put("dataType", dataType);
            paraMap.put("yearNum", yearNum);
            paraMap.put("monthOrWeekNum", monthOrWeekNum);
            paraMap.put("pointCodes", request.getParameter("pointCodes"));

            List<ContrastRatioEntity> thBList = null;
            // 国控点 或者 省控点 或者 小型站; 判断用户是否具有访问权限
            if ((SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("001"))
                || (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("002"))
                || (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("004"))) {
                thBList = gPointAirService.findGPointContrastRatioBySiteCodeAndTime(paraMap);
            } else {
                thBList = new ArrayList<ContrastRatioEntity>();
            }
            String siteStr = "";
            String dateStr = "";
            if (SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType)) {
                siteStr = "国控站";
            } else if (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType)) {
                siteStr = "省控站";
            } else if (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType)) {
                siteStr = "小型站";
            }
            //周报
            if (DataTypeEnum.WEEK_DATA.getValue().equals(dataType)) {
                dateStr = yearNum + "年第" + monthOrWeekNum + "周";
            } else if (DataTypeEnum.MONTH_DATA.getValue().equals(dataType)) {
                dateStr = yearNum + "年第" + monthOrWeekNum + "月";
            } else if (DataTypeEnum.YEAR_DATA.getValue().equals(dataType)) {
                dateStr = yearNum + "年";
            }
            wb = ExcelUtil.fillAirQualityContrastRatioExcelWithTemplate(thBList, "contrastRatioReportTemplate.xlsx",
                siteStr + dateStr + "空气质量同环比分析");
            ResponseUtil.export(response, wb, "空气质量同环比分析" + siteStr + dateStr + "数据.xlsx");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出空气质量同环比分析Excel异常,后台异常", e);
        }
    }


    /**
     * 导出城市空气质量排名Excel
     *
     * @param response
     * @param request
     * @return
     */
    @RequestMapping(value = "areaComprehensiveIndexDataExcelExport", method = RequestMethod.GET)
    public void areaComprehensiveIndexDataExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        // 排名范围类型: 全国排名 nationalSort;省内排名 provinceSort;区县排名 areaCountySort;办事处排名
        // officeSort
        String sortScopeType = request.getParameter("sortScopeType");
        if (null == sortScopeType || "".equals(sortScopeType)) {
            sortScopeType = SortScopeTypeEnum.NATIONAL_SORT.getValue();
        }

        // 数据类型 时报hourData；日报dayData；月报monthData；年报yearData
        // DataTypeEnum.HOUR_DATA.getValue()
        String dataType = request.getParameter("dataType");
        if (null == dataType || "".equals(dataType)) {
            dataType = DataTypeEnum.HOUR_DATA.getValue();
        }

        // 区域范围类型：全部allScope；市区 cityScope；郊县 outskirtsScope
        String scopeType = request.getParameter("scopeType");
        if (null == scopeType || "".equals(scopeType)) {
            scopeType = ScopeTypeEnum.ALL_SCOPE.getValue();
        }

        String startTime = request.getParameter("startTime");
        if (null == startTime || "".equals(startTime)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.HOUR_OF_DAY, -1);
            startTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_H);
        }
        String endTime = request.getParameter("endTime");
        if (null == endTime || "".equals(endTime)) {
            endTime = startTime;
        }

        // 封装request中的查询条件
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("sortScopeType", sortScopeType);
        paraMap.put("dataType", dataType);
        paraMap.put("scopeType", scopeType);
        paraMap.put("startTime", startTime);
        paraMap.put("endTime", endTime);
        try {
            List<IAQIEntity> iaqiList = null;
            // 全国、省内、区县排名，都在一个表中
            if (SortScopeTypeEnum.NATIONAL_SORT.getValue().equals(sortScopeType)
                || SortScopeTypeEnum.PROVINCE_SORT.getValue().equals(sortScopeType)
                || SortScopeTypeEnum.AREA_COUNTY_SORT.getValue().equals(sortScopeType)
                || SortScopeTypeEnum.OFFICE_SORT.getValue().equals(sortScopeType)) {
                iaqiList = cityAirService.findCityIAQIByRegionCodeAndTime(paraMap);
                // 办事处排名
                // 无效范围类型
            } else {
                iaqiList = new ArrayList<IAQIEntity>();
            }

            String typeStr = "";
            String areaTypeStr = "";
            if (SortScopeTypeEnum.NATIONAL_SORT.getValue().equals(sortScopeType)) {
                typeStr = "全国排名";
            } else if (SortScopeTypeEnum.PROVINCE_SORT.getValue().equals(sortScopeType)) {
                typeStr = "省内排名";
            } else if (SortScopeTypeEnum.AREA_COUNTY_SORT.getValue().equals(sortScopeType)) {
                if (ScopeTypeEnum.ALL_SCOPE.getValue().equals(scopeType)) {
                    areaTypeStr = "(全部)";
                } else if (ScopeTypeEnum.CITY_SCOPE.getValue().equals(scopeType)) {
                    areaTypeStr = "(市区)";
                } else if (ScopeTypeEnum.OUTSKIRTS_SCOPE.getValue().equals(scopeType)) {
                    areaTypeStr = "(郊县)";
                }
                typeStr = "区县排名";
            } else if (SortScopeTypeEnum.OFFICE_SORT.getValue().equals(sortScopeType)) {
                typeStr = "办事处排名";
            }
            String dateStr = "";
            String dataTypeStr = "";
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(CalendarUtil.getDateByString(startTime, DamConstant.ALL_SDF_YMD_H));
            String year = String.valueOf(calendar.get(Calendar.YEAR));
            String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
            String day = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
            String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
            if (DataTypeEnum.YEAR_DATA.getValue().equals(dataType)) {
                dataTypeStr = "年报";
                dateStr = year + "年";
            } else if (DataTypeEnum.MONTH_DATA.getValue().equals(dataType)) {
                dataTypeStr = "月报";
                dateStr = year + "年" + month + "月";
            } else if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {
                dataTypeStr = "日报";
                dateStr = year + "年" + month + "月" + day + "日";
            } else if (DataTypeEnum.HOUR_DATA.getValue().equals(dataType)) {
                dataTypeStr = "时报";
                dateStr = year + "年" + month + "月" + day + "日" + hour + "时";
            }

            wb = ExcelUtil.fillAreaComprehensiveIndexDataExcelWithTemplate(iaqiList, "airIndexDataTemplate.xlsx",
                dateStr + "空气质量" + typeStr + areaTypeStr + dataTypeStr);
            ResponseUtil.export(response, wb, "空气质量" + typeStr + areaTypeStr + dataTypeStr + ".xlsx");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出城市空气质量排名Excel异常，后台程序异常。", e);
        }
    }


    /**
     * 导出站点的 空气质量数据
     *
     * @param response
     * @param request
     * @return
     */
    @RequestMapping(value = "airQuarlityDataExcelExport", method = RequestMethod.GET)
    public void airQuarlityDataExcelExport(HttpServletRequest request, HttpServletResponse response) {
        // 点位数据：国控点 nationalSite;省控点 provinceSite；小型站 smallAirSite
        String pointType = request.getParameter("pointType");
        // 点位类型 默认 国控点 SiteTypeEnum.NATIONAL_SITE.getValue()
        if (null == pointType || "".equals(pointType)) {
            pointType = SiteTypeEnum.NATIONAL_SITE.getValue();
        }
        // 数据类型：小时数据 hourData；日数据 dayData；5分钟数据 minuiteData
        String dataType = request.getParameter("dataType");
        // 数据类型 默认 小时数据 DataTypeEnum.HOUR_DATA.getValue()
        if (null == dataType || "".equals(dataType)) {
            dataType = DataTypeEnum.HOUR_DATA.getValue();
        }

        // 数据计算类型：实时 realTimeData，累计 addUpTimeData
        String dataCalculateType = request.getParameter("dataCalculateType");
        // 数据类型 默认 实时 DataCalculateTypeEnum.REAL_TIME_DATA.getValue()
        if (null == dataCalculateType || "".equals(dataCalculateType)) {
            dataCalculateType = DataCalculateTypeEnum.REAL_TIME_DATA.getValue();
        }

        // 开始时间
        String startTime = request.getParameter("startTime");
        if (null == startTime || "".equals(startTime)) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.HOUR_OF_DAY, -1);
            startTime = CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_H);
        }

        // 结束时间
        String endTime = request.getParameter("endTime");
        if (null == endTime || "".equals(endTime)) {
            endTime = startTime;
        }

        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("pointType", pointType);
        paraMap.put("dataType", dataType);
        paraMap.put("dataCalculateType", dataCalculateType);
        paraMap.put("startTime", startTime);
        paraMap.put("endTime", endTime);
        paraMap.put("pointCodes", request.getParameter("pointCodes"));
        Workbook wb;
        try {

            List<AQIEntity> aqiList = null;

            // 国控点 或者 省控点 小型站 判断用户是否具有访问权限
            if ((SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("001"))
                || (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("002"))
                || SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType) && UserSiteUtil.userIsNotContainSiteTypes("004")) {
                aqiList = gPointAirService.findAirSiteAQIByTime(paraMap);
            } else {
                aqiList = new ArrayList<AQIEntity>();
            }

            String siteStr = "";
            String dateTypeStr = "";
            if (SiteTypeEnum.NATIONAL_SITE.getValue().equals(pointType)) {
                siteStr = "国控站";
            } else if (SiteTypeEnum.PROVINCE_SITE.getValue().equals(pointType)) {
                siteStr = "省控站";
            } else if (SiteTypeEnum.SMALL_AIR_SITE.getValue().equals(pointType)) {
                siteStr = "小型站";
            }
            String startTimeStr = "";
            String endTimeStr = "";

            if (DataTypeEnum.HOUR_DATA.getValue().equals(dataType)) {
                dateTypeStr = "小时";
                startTimeStr = startTime.substring(0, 16);
                endTimeStr = endTime.substring(0, 16);
            } else if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {
                dateTypeStr = "日";
                startTimeStr = startTime.substring(0, 10);
                endTimeStr = endTime.substring(0, 10);
            } else if (DataTypeEnum.MINUTE_DATA.getValue().equals(dataType)) {
                dateTypeStr = "分钟";
                startTimeStr = startTime.substring(0, 16);
                endTimeStr = endTime.substring(0, 16);
            }
            String dataCalculateTypeStr = "";
            if (DataCalculateTypeEnum.REAL_TIME_DATA.getValue().equals(dataCalculateType)) {
                dataCalculateTypeStr = "实时";
            } else if (DataCalculateTypeEnum.ADD_UP_TIME_DATA.getValue().equals(dataCalculateType)) {
                dataCalculateTypeStr = "累计";
            }

            wb = ExcelUtil.fillAirQualityDataExcelWithTemplate(aqiList, "airQuarlityDataTemplate.xlsx",
                siteStr + startTimeStr + "至" + endTimeStr + "空气质量" + dataCalculateTypeStr + dateTypeStr + "数据");
            ResponseUtil.export(response, wb, siteStr + "空气质量" + dataCalculateTypeStr + dateTypeStr + "数据.xlsx");
            // 返回
//            return MsgResult.okResult(aqiList);// standardSiteMongoDataService.findStandardSiteAirQuality(request);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出站点的空气质量数据Excel异常,后台异常.", e);
//            return MsgResult.errorResult("查询数据出现异常，后台异常。");
        }
    }


    /**
     * 导出污染源数据Excel
     *
     * @param request
     * @param response
     * @author 李亚坤
     * @createtime 2017年3月29日11:40:27
     */
    @RequestMapping(value = "industryPollutionDataExcelExport", method = RequestMethod.GET)
    public void industryPollutionDataExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {
            MsgResult msgResult = industryPollutionService.findIndustryPollutionBySiteCodeAndTime(request);
            List<PollutionIndustryEntity> resultList = (List) msgResult.getData();

            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");

            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.HOUR_OF_DAY, -1);
            if (null == endTime || "".equals(endTime)) {
                endTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_H);
            }

            if (null == startTime || "".equals(startTime)) {
                calendar.add(Calendar.DATE, -1);
                startTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), DamConstant.ALL_SDF_YMD_H);
            }
            // 数据类型：小时数据hourData;日数据 dayData
            String dataType = request.getParameter("dataType");
            dataType = null == dataType || "".equals(dataType) ? "hourData" : dataType;
            String dataTypeStr = "hourData".equals(dataType) ? "小时" : "日";
            String startTimeStr = "";
            String endTimeStr = "";
            if ("hourData".equals(dataType)) {
                startTimeStr = startTime.substring(0, 16);
                endTimeStr = endTime.substring(0, 16);
            } else if ("dayData".equals(dataType)) {
                startTimeStr = startTime.substring(0, 10);
                endTimeStr = endTime.substring(0, 10);
            }
            wb = ExcelUtil.fillIndustryPollutionDataExcelWithTemplate(resultList, "industryPollutionDataTemplate.xlsx",
                startTimeStr + "至" + endTimeStr + "工业废气数据" + dataTypeStr + "数据");
            ResponseUtil.export(response, wb, "工业废气数据" + startTimeStr + "至" + endTimeStr + dataTypeStr + "数据.xlsx");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出工业废气数据Excel异常,后台异常", e);
        }
    }

    /**
     * 污染源数据的扬尘数据 导入Excel
     *
     * @param request
     * @return
     * @author 李亚坤
     * @createtime 2017年4月5日15:47:43
     */
    @RequestMapping(value = "raiseDustDataExcelExport", method = RequestMethod.GET)
    public void raiseDustDataExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {
            // 数据类型
            String dataType = request.getParameter("dataType");
            dataType = null == dataType || "".equals(dataType) ? "hourData" : dataType;
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            Map<String, Object> paramMap = new HashMap<>();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.HOUR_OF_DAY, -1);

            endTime = StringUtils.isEmpty(endTime) ?
                CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_H) : endTime;
            calendar.add(Calendar.DATE, -1);
            startTime = StringUtils.isEmpty(startTime) ?
                CalendarUtil.getDateFormat(calendar.getTime(), DamConstant.ALL_SDF_YMD_H) : startTime;

            paramMap.put("dataType", dataType);
            paramMap.put("startTime", startTime);
            paramMap.put("endTime", endTime);
            paramMap.put("regionCode", request.getParameter("regionCode"));
            paramMap.put("companyName", request.getParameter("companyName"));
            paramMap.put("siteCodes", request.getParameter("siteCodes"));
            String startTimeStr = startTime.substring(0, 16);
            String endTimeStr = endTime.substring(0, 16);
            String dataTypeStr = "";
            if (DataTypeEnum.HOUR_DATA.getValue().equals(dataType)) {
                dataTypeStr = "小时";
            } else if (DataTypeEnum.DAY_DATA.getValue().equals(dataType)) {
                dataTypeStr = "日";
            }
            List<RaiseDustEntity> resultList = industryPollutionService.findRaiseDustData(paramMap);
            wb = ExcelUtil.fillRaiseDustDataExcelWithTemplate(resultList, "raiseDustDataTemplate.xlsx",
                startTimeStr + "至" + endTimeStr + "扬尘" + dataTypeStr + "数据");
            ResponseUtil.export(response, wb, "扬尘" + startTimeStr + "至" + endTimeStr + dataTypeStr + "数据.xlsx");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出扬尘数据Excel出现异常，后台程序异常。");
        }
    }

    /**
     * 导出工业废水数据Excel
     *
     * @param request
     * @param response
     * @author 李亚坤
     * @createtime 2017年5月10日15:33:54
     */
    @RequestMapping(value = "waterPollutionDataExcelExport", method = RequestMethod.GET)
    public void waterPollutionDataExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {

            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");

            Calendar calendar = Calendar.getInstance();

            if (null == endTime || "".equals(endTime)) {
                endTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00");
            }

            if (null == startTime || "".equals(startTime)) {
                // 近两天的数据
                calendar.add(Calendar.DAY_OF_MONTH, -2);
                startTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00");
            }
            String startTimeStr = startTime.substring(0, 16);
            String endTimeStr = endTime.substring(0, 16);
            // 数据类型：小时数据hourData;日数据 dayData
            String dataType = request.getParameter("dataType");
            dataType = null == dataType || "".equals(dataType) ? "hourData" : dataType;
            String dataTypeStr;
            if ("hourData".equals(dataType)) {
                dataTypeStr = "小时";
            } else {
                dataTypeStr = "日";
            }
            List<PollutionIndustryEntity> waterDataList = waterPollutionService.findWaterPollutionDataByPSCode(request);
            wb = ExcelUtil.fillWaterPollutionDataExcelWithTemplate(waterDataList, "waterPollutionDataTemplate.xlsx",
                startTimeStr + "至" + endTimeStr + "工业废水" + dataTypeStr + "数据");
            ResponseUtil.export(response, wb, "工业废水" + startTimeStr + "至" + endTimeStr + dataTypeStr + "数据.xlsx");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出工业废水数据Excel出现异常，后台程序异常。");
        }
    }

    /**
     * 导出水质断面数据Excel
     *
     * @param request
     */
    @RequestMapping(value = "waterSurfaceDataExcelExport", method = RequestMethod.GET)
    public void waterSurfaceDataExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {

            Calendar calendar = Calendar.getInstance();
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            if (null == endTime || "".equals(endTime)) {
                endTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00");
            }

            if (null == startTime || "".equals(startTime)) {
                // 近两天的数据
                calendar.add(Calendar.DAY_OF_MONTH, -2);
                startTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00");
            }
            // 数据类型：小时数据hourData;日数据 dayData
            String dataType = request.getParameter("dataType");
            dataType = null == dataType || "".equals(dataType) ? "小时数据" : "日数据";

            List<WaterSurfaceEntity> waterSurfaceList = waterSurfaceService.findWaterSurfaceDataByPSCode(request);

            wb = ExcelUtil.fillWaterSurfaceDataExcelWithTemplate(waterSurfaceList, "waterSurfaceDataTemplate.xlsx",
                startTime + "至" + endTime + "水质断面" + dataType);

            ResponseUtil.export(response, wb, "水质断面" + startTime + "至" + endTime + dataType + ".xlsx");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出水质断面数据Excel出现异常，后台程序异常。");
        }
    }

    /**
     * 导出油烟数据Excel
     *
     * @param request
     */
    @RequestMapping(value = "lampBlackDataExcelExport", method = RequestMethod.GET)
    public void lampBlackDataExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {

            Calendar calendar = Calendar.getInstance();
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            if (null == endTime || "".equals(endTime)) {
                endTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00");
            }

            if (null == startTime || "".equals(startTime)) {
                // 近两天的数据
                calendar.add(Calendar.DAY_OF_MONTH, -8);
                startTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00");
            }
            // 数据类型：小时数据hourData;日数据 dayData
            String dataType = request.getParameter("dataType");
            dataType = null == dataType || "".equals(dataType) ? "小时数据" : "日数据";

            List<LampBlackEntity> raiseDustEntities = lampBlackService.findLampBlackDataByRegionCode(request);
            wb = ExcelUtil.fillLampBlackDataExcelWithTemplate(raiseDustEntities, "lampBlackDataTemplate.xlsx",
                startTime + "至" + endTime + "油烟" + dataType);

            ResponseUtil.export(response, wb, "油烟" + startTime + "至" + endTime + dataType + ".xlsx");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出油烟数据Excel出现异常，后台程序异常。");
        }
    }

    /**
     * 导出喷漆房数据Excel
     *
     * @param request
     */
    @RequestMapping(value = "sprayBoothDataExcelExport", method = RequestMethod.GET)
    public void sprayBoothDataExcelExport(HttpServletRequest request, HttpServletResponse response) {
        Workbook wb;
        try {

            Calendar calendar = Calendar.getInstance();
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            if (null == endTime || "".equals(endTime)) {
                endTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00");
            }

            if (null == startTime || "".equals(startTime)) {
                // 近两天的数据
                calendar.add(Calendar.DAY_OF_MONTH, -2);
                startTime = DateTimeUtil.getFormatDateStr(calendar.getTime(), "yyyy-MM-dd HH:00");
            }
            // 数据类型：小时数据hourData;日数据 dayData
            String dataType = request.getParameter("dataType");
            dataType = null == dataType || "".equals(dataType) ? "小时数据" : "日数据";

            List<RaiseDustEntity> raiseDustEntities = new ArrayList<>();
            wb = ExcelUtil.fillSprayBoothDataExcelWithTemplate(raiseDustEntities, "sprayBoothDataTemplate.xlsx",
                startTime + "至" + endTime + "喷漆房" + dataType);

            ResponseUtil.export(response, wb, "喷漆房" + startTime + "至" + endTime + dataType + ".xlsx");

        } catch (Exception e) {
            e.printStackTrace();
            log.error("导出喷漆房数据Excel出现异常，后台程序异常。");
        }
    }


}
